package com.renting.modules.upyun.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.renting.modules.renting.rentSys.entity.House;
import com.renting.modules.renting.rentSys.entity.HouseImage;
import com.renting.modules.upyun.dao.ImageDao;
import com.renting.modules.upyun.utils.UpYunUtils;
import main.java.com.UpYun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class UpYunService {

    @Value("$up_bucket_url")
    private String UP_BUCKET_URL;//服务器前缀

    @Autowired
    ImageDao imageDao;

    @Autowired
    UpYun upYun;

    public String uploadYun(HouseImage image) {
        String visitUrl = UpYunUtils.getVisitUrl(UP_BUCKET_URL, image.getOriginName());
        image.setVisitUrl(visitUrl);
        image.setSuffix(UpYunUtils.getSuffix(image.getOriginName()));
        image.setServerUrl(visitUrl.split(UP_BUCKET_URL)[1]);
        image.setId(RandomUtil.simpleUUID());
        Object o = imageDao.save(image);//保存本地
        boolean result4 = upYun.writeFile("/renting/test.jpg", image.getServerUrl());
        return visitUrl;

    }




}
